<?php
namespace app\common\model;
use app\common\cache\CacheData;

class AdminAuth extends Base
{
	static $type_remark = [
            'api' => '查询',
            'handle' => '操作',
            'show' => '视图'
        ];
    static $is_public = [
            0 => '否',
            1 => '是'
    ];

    public function cacheData(){
        $data = $this->select();
        if($data){
        	foreach($data as &$value):
        		$value=$value->toArray();
        	endforeach;
        }
        return $data;
    }

    public static function type_remark($key=''){
        if(!$key){
            return '所有';
        }else{
            if(isset(self::$type_remark[$key])){
                return self::$type_remark[$key];
            }else{
                return '';
            }
        }
    }

    public static function is_public($key=null){
        if(is_null($key)){
            return self::$is_public;
        }else{
            if(isset(self::$is_public[$key])){
                return self::$is_public[$key];
            }else{
                return false;
            }
        }
    }
	
    public static function adminAuthData($id=null){
        $CACHE = CacheData::getInstance('AdminAuth');
        $cacheData = $CACHE -> readCache();
        $data = [];
        foreach($cacheData as $cache):
            $cache['type_name'] = self::type_remark($cache['type']);
            $cache['is_public_remark'] = self::is_public($cache['is_public']);
            $data[$cache['id']] = $cache;
        endforeach;
        if(!is_null($id)){
            if(isset($data[$id]))
                return $data[$id];
            return
                false;
        }else{
            return $data;
        }
        
    }

    /**
     * 根据规则名称获取规则
     * @param [type] $auth_path [description]
     */
    public static function authByAuthPath($auth_path){
        $authes = self::adminAuthData();
        foreach($authes as $auth):
            if($auth['auth_path']==$auth_path){
                return $auth;
                break;
            }
        endforeach;
        return false;
    }

}